<?php
/**
 * Powered By ThinkPHP 5.0.12
 * Copyright (c) 2017 http://www.bojuwang.net All rights reserved.
 * Created by PhpStorm.
 * Date: 2018/4/26 0026
 * Time: 上午 10:29
 * Author: NanTao <260053530@qq.com>
 */

namespace app\bjwlmanager\controller;
use think\Db;
class Distribution extends ManagerBasic
{
    public function index()
    {

        return $this->fetch();
    }

    public function getTree($data){
        $ret=$this->reSort($data);
        return $ret;
    }
    public function reSort($data,$user_id=0,$class_layer=0){
        static $ret = array();
        foreach ($data as $k => $v){
            if($v['next_user_id']==$user_id){
                $v['class_layer'] = $class_layer;
                $ret[] = $v;
                $this->reSort($data,$v['next_user_id'],$class_layer+1);
            }
        }
        return $ret;
    }


    public function getuserlist(){
        $data = Db::name('user_distribution')
            ->alias('b')
            ->field('a.user_name,a.user_nickname,a.mobile,a.type,a.add_ip,a.add_time,b.*')
            ->join('user a','a.id=b.next_user_id')
            ->select();
        echo '<pre/>';
//        print_r($data);die;
        $res = $this->getUserData($data,0,0);
        print_r($res);die;
    }

    public function getUserData($list,$id='',$rank,$qian)
    {
        $data  = [];
        if($id){
            foreach($list as $k=>$v){
                if ($v['user_id'] == $id ){
                    $v['user_nickname'] =  $v['user_nickname'].$qian;
                    $v['rank'] =  $rank;
                    $data[] = $v;
                    $data = array_merge($data,$this->getUserData($list,$v['next_user_id'],$rank+1,$qian.'$nbsp;'));
                }
            }
        }else{
            $v = $list[0];
            $v['user_nickname'] =  $v['user_nickname'].$qian;
            $v['rank'] =  $rank;
            $data[] = $v;
            $data = array_merge($data,$this->getUserData($list,$v['next_user_id'],$rank+1,$qian.'$nbsp;'));
        }

        return $data;


    }


    public function test()
    {
        $str = 'SELECT m.aid,m.b1id,n.bcon,m.b2id as b2id,o.bcon as b2con, m.b3id as b3id,p.bcon as b3con FROM `test` as m INNER JOIN test1 as n on m.b1id=n.bid INNER JOIN test1 as o on m.b2id=o.bid INNER JOIN test1 as p on m.b3id=p.bid;';
    }
}